package com.hkbigdata.sybx.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @author yangjing
 * @creat 2025-05-06-13:17
 * @email 3010335359@qq.com
 */

@Configuration
public class CorsConfig {
    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        // 使用 addAllowedOriginPattern 替代 addAllowedOrigin
        config.addAllowedOriginPattern("http://localhost:8080");
        config.addAllowedOriginPattern("http://localhost:5173");
        config.addAllowedMethod("*");
        config.addAllowedHeader("*");
        // 允许携带凭证
        config.setAllowCredentials(true);

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);

        return new CorsFilter(source);
    }
}